package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;
import java.util.Map;

//加事务
@Transactional
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {


        //判断预约的日期医院知否开放,改成日期格式,
        String orderDate = (String) map.get("orderDate");//预约日期
        //时间转换工具类
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));

        if (orderSetting == null) {
            //指定日期没有预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }


        //判断该日期是否预约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            //预约满,无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断用户同一天是否重复预约,根据手机号
        String telephone = (String) map.get("telephone");//获取客户手机号
        Member member = memberDao.findByTelephone(telephone);
        //会员信息不是空,查询是不是今天添加过了,根据三个条件,1.会员套餐2.会员ID3.预约日期
        if (member != null) {
            //判断是否在重复添加
            //会员ID
            Integer id = member.getId();
            //预约日期
            Date order_Data = DateUtils.parseString2Date(orderDate);
            //套餐ID,传过来的map里面有
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(id, order_Data, Integer.parseInt(setmealId));

            //查询有结果就是重复预约
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
                //已重复预约,无法完成预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        //判断用户是否是会员,是会员预约成功,不是会员自动注册会员
        member = new Member();
        member.setName((String) map.get("name"));
        member.setPhoneNumber(telephone);
        member.setIdCard((String) map.get("idCard"));
        member.setSex((String) map.get("sex"));
        member.setRegTime(new Date());
        memberDao.add(member);//自动完成会员注册


        //预约成功,更新当日已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());


    }

    //根据预约ID查询预约相关信息（体检人姓名、预约日期、套餐名称、预约类型）
    public Map findById(Integer id) throws Exception{
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
